package com.chudian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chudian.entity.OrderInfo;
import com.chudian.entity.Product;
import com.chudian.enums.OrderStatus;
import com.chudian.enums.wxpay.WxTradeState;
import com.chudian.mapper.OrderInfoMapper;
import com.chudian.mapper.ProductMapper;
import com.chudian.service.OrderInfoService;
import com.chudian.util.OrderNoUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.time.Instant;
import java.util.List;

@Service
@Slf4j
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {

    @Autowired
    private ProductMapper productMapper;

    /**
     * 根据商品id生成订单
     * @param productId
     * @return
     */
    public OrderInfo createOrderInfoByProductId(Long productId,String paymentType) {

        //获取已存在但未支付订单
        OrderInfo orderInfo = this.getNopayOrderInfoByProductId(productId,paymentType);
        if(orderInfo!=null){
            return orderInfo;
        }

        //获取商品信息
        Product product = productMapper.selectById(productId);
        //生成订单
        orderInfo=new OrderInfo();
        orderInfo.setOrderNo(OrderNoUtils.getOrderNo());
        orderInfo.setTitle(product.getTitle());
        orderInfo.setTotalFee(product.getPrice()); //分
        orderInfo.setProductId(productId);
        orderInfo.setOrderStatus(OrderStatus.NOTPAY.getType());
        orderInfo.setPaymentType(paymentType);
        //存入数据库
        baseMapper.insert(orderInfo);

        return orderInfo;
    }

    /**
     * 保存二维码信息
     * @param orderNo
     * @param codeUrl
     */
    @Override
    public void saveCodeUrl(String orderNo, String codeUrl) {
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no", orderNo);

        OrderInfo orderInfo=new OrderInfo();
        orderInfo.setCodeUrl(codeUrl);

        baseMapper.update(orderInfo, queryWrapper);
    }

    /**
     * 根据创建时间对订单倒序排序
     * @return
     */
    @Override
    public List<OrderInfo> getOrderInfoByCreatetimeDesc() {
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        List<OrderInfo> list = baseMapper.selectList(queryWrapper);
        return list;
    }

    /**
     * 根据订单编号更新订单状态
     * @param orderNo
     * @param orderStatus
     */
    @Override
    public void updateOrderStatusByOrderno(String orderNo, String orderStatus) {

        log.info("更新订单状态====>{}",orderStatus);

        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no", orderNo);

        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderStatus(orderStatus);

        baseMapper.update(orderInfo,queryWrapper);
    }

    /**
     * 根据订单号获取订单状态
     * @param orderNo
     * @return
     */
    public String getOrderState(String orderNo) {
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no", orderNo);
        OrderInfo orderInfo = baseMapper.selectOne(queryWrapper);
        //防止被删除的订单的回调通知的调用
        if(orderInfo==null){
            return null;
        }
        return orderInfo.getOrderStatus();
    }

    /**
     * 找出创建超过minutes分钟并且未支付的订单
     * @param minutes
     * @return
     */
    @Override
    public List<OrderInfo> queryNoPayOrderDuration(int minutes,String paymentType) {
        //创建minutes分钟之前的时间实例
        Instant minus = Instant.now().minus(Duration.ofMinutes(minutes));

        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_status", OrderStatus.NOTPAY.getType());
        queryWrapper.le("create_time", minus);
        queryWrapper.eq("payment_type",paymentType);

        List<OrderInfo> orderInfoList = baseMapper.selectList(queryWrapper);
        return orderInfoList;
    }

    /**
     * 根据订单号获取订单信息
     * @param orderNo
     * @return
     */
    @Override
    public OrderInfo getOrderInfoByOrderNo(String orderNo) {
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no", orderNo);

        OrderInfo orderInfo = baseMapper.selectOne(queryWrapper);
        return orderInfo;
    }

    /**
     * 根据productId查询未支付订单
     * 防止重复创建订单对象
     * @param productId
     * @return
     */
    private OrderInfo getNopayOrderInfoByProductId(Long productId,String paymentType) {
        //封装查询条件
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id", productId);
        queryWrapper.eq("order_status", OrderStatus.NOTPAY.getType());
        queryWrapper.eq("payment_type", paymentType);
        //queryWrapper.eq("user_id", userId);
        //执行查询
        OrderInfo orderInfo = baseMapper.selectOne(queryWrapper);
        return orderInfo;
    }
}
